<?php
namespace News\Controller;
use Think\Controller;
/*
	关键字添加
*/
class RuleController extends InitController {

	/**
	*	添加关键字
	*/
	public function PostKeyword(){
		
		$topmenu = $this->topmenu;
		$sidebarmenu = $this->sidebarmenu[I('get.k')];
		
		//关键字返回
		$info = D('Rule')->DisplayRule();

		if($info['status']){
			$ruleinfo = is_array($info['info']) ? $info['info'] : array();
			$rid = $info['rule_id'];
			$where = array(
				'rule_id'=>$rid,
			);
			$result = M('NewsReply')->where($where)->order('parentid asc , id asc')->select();
		
			$reply = array();
			if (!empty($result)) {
				foreach ($result as $index => $row) {
					if (empty($row['parentid'])) {
						$reply[$row['id']] = $row;
					} else {
						$reply[$row['parentid']]['children'][] = $row;
					}
				}
			}
		}
		
		if(IS_AJAX){
			
			$newspicture	= I('newspicture','','trim');
			$newsurl        = I('newsurl','','trim');
			$newstitle      = I('newstitle','','trim');
			
			if(empty($newstitle)){
				$this->ajaxReturn(array('info'=>'标题不能为空','status'=>'0'));
			}
			
			if(empty($newspicture)){
				$this->ajaxReturn(array('info'=>'上传图片不能为空','status'=>'0'));
			}
			
			/* if(empty($newsurl)){
				$this->ajaxReturn(array('info'=>'链接地址不能为空','status'=>'0'));
			} */
			//判断是否是正确的url链接
			/* if(!isUrl($newsurl)){
				$this->ajaxReturn(array('info'=>'请输入正确的链接','status'=>'0'));
			} */
			
			
			//规则添加
			$Rule = D('Rule')->Post();
			if($Rule['status']){
				D('NewsReply')->FormSubmit($Rule['rule_id']);
			}
		
			$this->ajaxReturn($Rule);

		}
		
		
		$this->assign('topmenu',$topmenu);
		$this->assign('sidebarmenu',$sidebarmenu);
		
		$this->assign(array(
			'reply'=>$reply,
			'ruleinfo'=>$ruleinfo,
			'result'=>$result
		));
		$this->display('PostKeyword',U('RuleDisplay'));

	}


	//显示列表
	public function RuleDisplay(){		
	
		$topmenu = $this->topmenu;
		$sidebarmenu = $this->sidebarmenu[I('get.k')];
		
		$title = I('title');
		$where = array(
			'nr.parentid'=>0,
			'r.module'=>'news',	
		);
		
		if($title){
			$where['r.name'] = array('like','%'.$title.'%');
		}
	
		$total = M('Rule')->alias('r')->join("left join __NEWS_REPLY__ AS nr on r.id = nr.rule_id")->where($where)->count();
		
		$page = new \Think\Page($total,10);
		$pager = $page->show();

		$ruleall = M('Rule')->alias('r')->join("left join __NEWS_REPLY__ AS nr on r.id = nr.rule_id")->where($where)->order('r.id desc')->field('r.id,r.name')->limit($page->firstRow.",".$page->listRows)->select();
		
		$list=array();
		if ($ruleall) {
			foreach($ruleall as $kv => $ra){
				$list[$kv] = $ra;
				$list[$kv]['keywords'] = M('RuleKeyword')->where("rule_id='{$ra['id']}'")->select();
			}
		}
		
		$this->assign(array(
			'pager'=>$pager,
			'list'=>$list,
		));
		$this->assign('topmenu',$topmenu);
		$this->assign('sidebarmenu',$sidebarmenu);
		
		$this->display('RuleDisplay');
	}

    /**
    *
    * 删除规则
    */
    public function RuleDelete() {

    	$id = intval(I('id'));
    	if(empty($id)){
    		$this->error('出错了哦！～');
    	}

    	//规则删除
		$RuleReturn = D('Rule')->DeleteRule();
		if(!$RuleReturn['status']){
			$this->error($RuleReturn['info']);
		}

    	$row = M('NewsReply')->where(array('rule_id'=>$id))->field("id, parentid, rule_id, thumb")->find();
		if (empty($row)) {
			$this->error('抱歉，回复不存在或是已经被删除！');
		}

		$ac = M('NewsReply')->where(array('rule_id' => $id))->delete();
		if ( $ac ) {

			if ($row['parentid'] == 0) {
				 M('NewsReply')->where(array('parentid' => $row['id']))->delete();
			}

			$this->success('删除成功');die;
		}
		$this->error('删除失败！');
    }

}